//
//  PostImageOfPersonCardVC.m
//  EJLX
//
//  Created by adc on 15/11/12.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "PostImageOfPersonCardVC.h"
#import "BankListVC.h"
@interface PostImageOfPersonCardVC ()
@property(nonatomic)NSMutableArray * provinceNames;
@property(nonatomic)NSMutableArray * cityNames;
@end

@implementation PostImageOfPersonCardVC
-(void)viewWillAppear:(BOOL)animated{
    if (_bankStr != nil) {
        NSLog(@"333 %@",_bankStr);
    }
    [_tableView reloadData];
    
}
-(void)setBankListName:(NSString *)bankListName{
    bankListName = @"123456name";
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    _isSelf = YES;
    

    self.view.backgroundColor = [UIColor whiteColor];
    
     _titleArray = @[@"姓名",@"银行卡号",@"所属银行",@"支行",@"支行"];

    [self getTheDictionaryForCity];
    [self creatNav];
   
    [self addTableView];
   

}
-(void)creatNav{
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDE, 44)];
    navView.backgroundColor = RGB_COLOR(0x00c49f);
    [self.view addSubview:navView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 7, 30, 30);
    leftBtn.layer.cornerRadius = 20;
    [leftBtn setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal] ;
    [leftBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftBtn];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-60, 7, 120, 20  )];
    title.text = @"银行卡";
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [navView addSubview:title];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(WIDE-50, 7, 30, 30);
    rightBtn.layer.cornerRadius = 20;
    [rightBtn setTitle:@"提交"  forState:UIControlStateNormal] ;
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:12.f];
    [rightBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:rightBtn];
    
}

-(void)backBtn{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)addTableView{
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, WIDE, 44*_titleArray.count) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    [self.view addSubview:_tableView];
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    NSArray *placeholderTF = @[@"输入持卡人姓名",@"输入您的银行卡号",@"选择银行",@"选择支行所在的省份／市区",@"所属支行 如:苏站路支行"];
    UserTableViewCell *cell = nil;
    
        cell = [[UserTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell = [[NSBundle mainBundle] loadNibNamed:@"UserTableViewCell" owner:nil options:nil][0];
        
        cell.addInfoNameLB.text = _titleArray[indexPath.row];
        cell.addInfoNameLB.textAlignment = NSTextAlignmentLeft;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.addInfoForPeopleTF.placeholder = placeholderTF[indexPath.row];
        cell.addInfoForPeopleTF.borderStyle = UITextBorderStyleNone;
        cell.addInfoForPeopleTF.tag = indexPath.row;
    switch (indexPath.row) {
        case 0:
            cell.addInfoForPeopleTF.text = _name;
            break;
        case 1:
            cell.addInfoForPeopleTF.text = _card;
            break;
        case 2:
            cell.addInfoForPeopleTF.text = _bankStr;
            cell.addInfoForPeopleTF.userInteractionEnabled = NO;

            break;
        case 3:
            cell.addInfoForPeopleTF.text = _pickString;
            cell.addInfoForPeopleTF.adjustsFontSizeToFitWidth = YES;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.addInfoForPeopleTF.userInteractionEnabled = NO;
            
            break;
        case 4:
            cell.addInfoForPeopleTF.text = _address;
            break;
            
        default:
            break;
    }

        [cell.addInfoForPeopleTF addTarget:self action:@selector(selectCityBtnClick:) forControlEvents:UIControlEventEditingDidEnd];
    
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 2){
        
            if (_isSelf) {
                //选择银行
                BankListVC *bank = [[BankListVC alloc] init];
                bank.delegate = self;
                if (!bank) {
                    
                }
                [self presentViewController:bank animated:YES completion:nil];
                _isSelf = NO;
            }
            
       
    }
    if (indexPath.row == 3) {
        [_pickerView removeFromSuperview];
        _pickerView = nil;
        if (!_pickerView) {
            _pickerView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT, WIDE, 200)];
            
            _pickerView.backgroundColor = [UIColor orangeColor];
            [UIView animateWithDuration:0.1 animations:^{
                _pickerView.frame = CGRectMake(0, HEIGHT-200, WIDE, 200);
                
                [self.view addSubview:_pickerView];
                for (int i=0; i<2; i++) {
                    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
                    btn.frame = CGRectMake((WIDE-50)*i, 0, 50, 20);
                    NSString *str =(i==0)?@"取消":@"确定";
                    
                    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    [btn setTitle:str forState:UIControlStateNormal];
                    btn.tag = 10000+i;
                    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
                    [_pickerView addSubview:btn];
                    
                }
                _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 20, WIDE, 180)];
                _picker.showsSelectionIndicator = YES;
                _picker.backgroundColor = [UIColor whiteColor];
                _picker.delegate = self;
                _picker.dataSource = self;
                [_pickerView addSubview:_picker];
                
            } ];
        }

    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - picker
-(void)selectCityBtnClick:(UITextField *)TF{

    if (TF.tag ==0){
        _name = TF.text;
        
    }else if (TF.tag ==1){
        _card = TF.text;
    }else if (TF.tag ==4){
       _address = TF.text;
    }
    [_pickerView removeFromSuperview];
    [_tableView reloadData];
}
-(void)getBankName:(NSString *)bankName andCode:(NSString *)code{
    _isSelf = YES;
    _bankStr = bankName;
    _bankCode = code;
    [_tableView reloadData];
   
   
}
-(void)btnClick:(UIButton *)btn{
    if (btn.tag == 10001){
        _pickString = [NSString stringWithFormat:@"%@%@%@",_sheng,_shi,_qu];
        
    }
    [UIView animateWithDuration:0.1 animations:^{
        _pickerView.frame = CGRectMake(0, HEIGHT, WIDE, 200);
    }completion:^(BOOL finished) {
        _isExit = NO;
        [_pickerView removeFromSuperview];
    }];
    [_pickerView removeFromSuperview];
    [_tableView reloadData];
}

-(NSMutableArray * )provinceNames{
    if(!_provinceNames){
        _provinceNames = [NSMutableArray array];
    }
    return _provinceNames;
}
-(NSMutableArray * )cityNames{
    if(!_cityNames){
        _cityNames = [NSMutableArray array];
    }
    return _cityNames;
}

-(void)getTheDictionaryForCity{
    //加载数据

    PraseProvince *parse = [PraseProvince manager];
    _provinces = parse.provinces;

   
    for (NSString* pn in _provinces.allKeys) {
        [self.provinceNames addObject:pn];
    }
   
        NSString * provinceName = _provinceNames[0];
        _cities = _provinces[provinceName];
    for (NSString * cn in _cities.allKeys) {
        [self.cityNames addObject:cn];
    }
    _areas = _cities[_cityNames[0]];
    
  }

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *pickLB = (UILabel *)view;
    if (!pickLB) {
        pickLB = [[UILabel alloc] init];
        pickLB.adjustsFontSizeToFitWidth = YES;
        pickLB.textAlignment = NSTextAlignmentCenter;
       [pickLB setBackgroundColor:RGB_COLOR(0x00c49f)];
        [pickLB setFont:[UIFont boldSystemFontOfSize:20]];
    }
    pickLB.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickLB;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    
    switch (component) {
        case 0:
            return [_provinceNames count];
            break;
        case 1:
            return [_cities count];
            break;
        case 2:
            return [_areas count];
            break;
            
        default:
            return 0;
            break;
    }
    
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    switch (component) {
        case 0:
            _sheng = _provinceNames[row] ;
            return _provinceNames[row] ;//objectForKey:@"state"];
       
            break;
        case 1:
            _shi = _cityNames[row];
            return _cityNames[row];//objectForKey:@"city"];
            break;
        case 2:
            _qu = _areas.allKeys[row];
            return _areas.allKeys[row];
        default:
            return  @"";
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (component) {
        case 0:{
            _sheng = [_provinceNames objectAtIndex:row] ;
            
            _cities = [_provinces objectForKey:_sheng];
            self.cityNames = nil;
            for (NSString * cn in _cities.allKeys) {
                [self.cityNames addObject:cn];
            }
            [self.picker selectRow:0 inComponent:1 animated:YES];
            [self.picker reloadComponent:1];//更新第二个数据
            
            NSString * cityName = _cityNames.firstObject;
            _areas = _cities[cityName];//objectForKey:@"areas"];
            
            [self.picker selectRow:0 inComponent:2 animated:YES];
            [self.picker reloadComponent:2];
        }
            break;
        case 1:
            _areas = _cities[_cityNames[row]] ;// objectForKey:@"areas"];
            [self.picker selectRow:0 inComponent:2 animated:YES];// index
            [self.picker reloadComponent:2];
            break;
        default:
            break;
    }
    //    if ([self.delegate respondsToSelector:@selector(pickerDidChaneStatus:)]) {
    //        [self.delegate pickerDidChaneStatus:self];
    //    }
}

-(void)save{
    //上传
    
    NSString *addr = [NSString stringWithFormat:@"%@,%@,%@",_sheng,_shi,_qu];
        [HttpsManager postBankCard:_card name:_name bank:_bankCode location:addr zhihang:_address completionBlock:^(NSString *msg) {
            if ([msg isEqualToString:@"ok"]) {
                SHOW_ALERT(@"上传成功")
                if (self.returnBlock) {
                    self.returnBlock(@"已完善");
                }
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                SHOW_ALERT(msg)
                    
            }
        }];
        NSLog(@"add %@ .....%@",addr,_address);

    
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
@end
